export default class Order {
	constructor(id, orderNo, createTime, status, amount, productName = '', productCover = '', orderStatus = '', payAmount = '') {
		this.id = id;
		this.orderNo = orderNo;
		this.createTime = createTime;
		this.status = status;
		this.amount = amount;
		this.productName = productName;
		this.productCover = productCover;
		this.orderStatus = orderStatus;
		this.payAmount = payAmount;
	}
	
	// 从JSON字符串解析单个订单对象
	static parseFromJsonString(jsonString) {
		let jsonObject = JSON.parse(jsonString);
		return new Order(
			jsonObject.id,
			jsonObject.orderNo,
			jsonObject.createTime,
			jsonObject.status || jsonObject.orderStatus,
			jsonObject.amount || jsonObject.payAmount,
			jsonObject.productName,
			jsonObject.productCover,
			jsonObject.orderStatus,
			jsonObject.payAmount
		);
	}
	
	// 从单个JSON对象解析订单
	static parseFromJsonObject(jsonObject) {
		return new Order(
			jsonObject.id,
			jsonObject.orderNo,
			jsonObject.createTime,
			jsonObject.status || jsonObject.orderStatus,
			jsonObject.amount || jsonObject.payAmount,
			jsonObject.productName,
			jsonObject.productCover,
			jsonObject.orderStatus,
			jsonObject.payAmount
		);
	}
	
	// 从订单列表数据解析为Order对象列表
	static parseOrderList(orderListData) {
		// 确保输入是数组
		if (!Array.isArray(orderListData)) {
			console.error('Order.parseOrderList: 输入数据不是数组', orderListData);
			return [];
		}
		
		// 遍历数组，将每个对象解析为Order实例
		return orderListData.map(orderData => {
			try {
				return Order.parseFromJsonObject(orderData);
			} catch (error) {
				console.error('解析订单数据失败:', error, orderData);
				return null;
			}
		}).filter(Boolean); // 过滤掉可能的null值
	}
}